package hbt;

import modelo.ItemAlmacen;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class HibernateAlmacenDAO {
	private static HibernateAlmacenDAO instancia = null;
	private static SessionFactory sf = null;

	public static HibernateAlmacenDAO getInstancia() {
		if (instancia == null) {
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernateAlmacenDAO();
		}
		return instancia;
	}

	public void altaItemAlmacen(ItemAlmacen ia) {
		// TODO Auto-generated method stub
		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(ia);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void actualizarItemAlmacen(ItemAlmacen ia) {
		// TODO Auto-generated method stub
		Session session = sf.openSession();
		session.beginTransaction();
		session.merge(ia);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public ItemAlmacen buscarItemAlmacen(String codigoFabricante) {
		// TODO Auto-generated method stub
		Session session = sf.openSession();

		ItemAlmacen ia = (ItemAlmacen) session
				.createQuery(
						"from ItemAlmacen ia join Rodamiento rod"
								+ "where rod.codigoFabricante = ?")
				.setString(0, codigoFabricante).list();

		session.close();
		return ia;
	}

}
